// pages/chatRoom/chatRoom.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focusList: null, //获取关注的人
    allList: null, //获取所有
    user: [{
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "10",
      title: "标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题",
      gender: 1,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "8",
      title: "标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题",
      gender: 1,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "25",
      title: "标题标题标题标题标题标标题标题标题标题标题标题标题标题标题题标题标题标题",
      gender: 0,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "99",
      title: "标题标题标题标题标题标题标题标题标题",
      gender: 1,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "45",
      title: "标题标题标题标题标题标题标题标题标题",
      gender: 0,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "87",
      title: "标题标题标题标题标题标题标题标题标题",
      gender: 1,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "95",
      title: "标题标题标题标题标题标题标题标题标题",
      gender: 0,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "21",
      title: "标题标题标题标题标题标题标题标题标题",
      gender: 1,
    }, {
      img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599630642706&di=23d0d78a818e014557846d91a499166f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F18%2F20181118142953_yaktg.thumb.700_0.jpeg",
      name: "昵称昵称昵称昵称昵称昵称昵称昵称昵称昵称",
      watchNum: "47",
      title: "标题标题标题标题标题标题标题标题标题",
      gender: 1,
    }],


    roomRange: null,
    attention: null,
  },

  // 获取关注的人
  getFocus() {
    // var focusList = this.data.user
    // focusList.forEach((items, i) => {
    //   items.color = app.util.getColor()
    // })
    // this.setData({
    //   focusList: focusList
    // })



    var postObj = {
      url: '/room/attention/get',
      method: 'POST',
      data: {
        userId: wx.getStorageSync('userId'),
        token: wx.getStorageSync('token')
      }
    }
    app.request.noLoginRequest(postObj).then(res => {
      console.log('获取关注的人res', res)
      var focusList = res.data.data
      if (focusList.length>0) {
        focusList.forEach((items, i) => {
          items.color = app.util.getColor()
        })
      }
      this.setData({
        focusList:focusList
      })
    }).catch(err => {
      console.log('获取关注的人err', err)
    })


  },
  // 获取所有 获取房间榜单接口
  getAll() {
    var allList = this.data.user
    allList.forEach((items, i) => {
      items.color = app.util.getColor()
    })
    this.setData({
      allList: allList
    })

    var postObj = {
      url: '/room/range/get/general',
      method: 'POST',
      data: {
        rangeType: "month"
      }
    }
    app.request.noLoginRequest(postObj).then(res => {
      console.log('获取房间榜单接口res', res)
      var roomRange = res.data.data.roomRange
      if (roomRange.length > 0) {
        roomRange.forEach((items, i) => {
          items.color = app.util.getColor()
          items.gain = app.util.numTransform(items.gain)
        })
      }
      this.setData({
        roomRange: roomRange
      })
    }).catch(err => {
      console.log('获取房间榜单接口err', err)
    }) 

  },
  // 点击复制链接  打开陌滋
  textPaste() {
    wx.setClipboardData({
      data: app.util.config().appUrl,
      success: function (res) {
        app.success("APP链接已复制!")
        setTimeout(() => {
          app.success("前往APP体验!")
        }, 1000)
      }
    })
  },
  // 跳转到拼团vip
  toVip(){
    // 判断登陆
    var token =  wx.getStorageSync('token')
    var userId =  wx.getStorageSync('userId')

    if (!!token&&!!userId) {
      wx.navigateTo({
        // url: '/pages/bargain/bargain'
        url: '/pages/bargain/myBargain'
        
      })
    }else{
      app.error("请先登陆!")
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/index/index'
        })
      }, 1000)
    }
    
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.util.getColor())

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!!wx.getStorageSync('userId') && !!wx.getStorageSync('token')) {
      this.getFocus()
    }

    this.getAll()
    
    if (!!wx.getStorageSync('token') && !!wx.getStorageSync('userId')) {
      app.imInit.imLogin()
      setTimeout(() => {
        // app.imInit.watchMessage()
      }, 1000)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.imInit.onMessageReceived()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})